package net.huashitong.ssydt.provider.pk.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huashitong.ssydt.pk.entity.vo.*;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.pk.service.YzddService;
import net.huashitong.ssydt.provider.vo.BaseLongIdPagingVO;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.18
 */
@Api(value = "/api/pk/yzdd", tags = {"一站到底"})
@RestController
@RequestMapping("/api/pk/yzdd")
public class ApiPkYzddController {

    private YzddService yzddService;

    @Autowired
    public ApiPkYzddController(YzddService yzddService) {
        this.yzddService = yzddService;
    }


    @ApiOperation(value = "获取挑战信息", notes = "获取挑战信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping
    public Result<GameRuleVO> get() {
        GameRuleVO gameRuleVO = yzddService.get();
        return ResultUtils.getSuccessResults(gameRuleVO);
    }

    @ApiOperation(value = "获取剩余免费挑战次数", notes = "获取剩余免费挑战次数", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/free")
    public Result<Integer> getFree() {
        String userId = UserUtils.getUserId();
        Integer freeTimes = yzddService.getFree(userId);
        return ResultUtils.getSuccessResults(freeTimes);
    }

    @ApiOperation(value = "获取距离比赛结束时间", notes = "获取距离比赛结束时间（单位：秒）", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/challenge/timer")
    public Result<Integer> getTimer() {
        int timer = yzddService.getTimer();
        return ResultUtils.getSuccessResults(timer);
    }

    @ApiOperation(value = "获取我的排行", notes = "获取我的排行", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/my/rank")
    public Result<MyRankVO> getMyRank() {
        String userId = UserUtils.getUserId();
        MyRankVO myRankVO = yzddService.getMyRank(userId);
        return ResultUtils.getSuccessResults(myRankVO);
    }


    @ApiOperation(value = "去除错误答案", notes = "去除错误答案", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/remove-error")
    public Result<String> postRemoveError(@RequestBody ChallengePrivilegeVO challengePrivilegeVO) {
        String userId = UserUtils.getUserId();
        yzddService.postRemoveError(challengePrivilegeVO, userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "选中正确答案", notes = "选中正确答案", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/selected-correct")
    public Result<String> postSelectedCorrect(@RequestBody ChallengePrivilegeVO challengePrivilegeVO) {
        String userId = UserUtils.getUserId();
        yzddService.postSelectedCorrect(challengePrivilegeVO, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "继续刷题（复活）", notes = "继续刷题（复活）", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/revive")
    public Result<String> postRevive(@RequestBody ChallengePrivilegeVO challengePrivilegeVO) {
        String userId = UserUtils.getUserId();
        yzddService.postRevive(challengePrivilegeVO, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "比赛刷题", notes = "比赛刷题", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/challenge")
    public Result<ChallengeRecordVO> postChallenge(YzddChallengeVO yzddChallengeVO) {
        String userId = UserUtils.getUserId();
        ChallengeRecordVO vo = yzddService.postChallenge(userId, yzddChallengeVO);
        return ResultUtils.getSuccessResults(vo);
    }

    @ApiOperation(value = "比赛交卷", notes = "比赛交卷", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @PostMapping("/challenge/submit")
    public Result<ChallengeRecordVO> postChallengeSubmit(@RequestBody ChallengeSubmitVO challengeSubmitVO) {
        String userId = UserUtils.getUserId();
        ChallengeRecordVO vo = yzddService.postChallengeSubmit(challengeSubmitVO, userId);
        return ResultUtils.getSuccessResults(vo);
    }

    @ApiOperation(value = "获取我的奖牌", notes = "获取我的奖牌", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @LoginRequired
    @GetMapping("/my/medal")
    public Result<MyMedalVO> getMyMedal() {
        String userId = UserUtils.getUserId();
        MyMedalVO vo = yzddService.getMyMedal(userId);
        return ResultUtils.getSuccessResults(vo);
    }


    @ApiOperation(value = "获取奖牌榜列表", notes = "获取奖牌榜列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/medal/rank")
    public Result<ArrayList<MedalVO>> getMedalRank(@Valid BaseLongIdPagingVO baseLongIdPagingVO) {
        List<MedalVO> list = yzddService.getMedalRank(baseLongIdPagingVO);
        return ResultUtils.getSuccessResultList(list);
    }

    @ApiOperation(value = "获取排行榜列表", notes = "获取排行榜列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/rank")
    public Result<ArrayList<RankingVO>> getRanking(@Valid RankingInVO rankingInVO) {
        List<RankingVO> list = yzddService.getRanking(rankingInVO);
        return ResultUtils.getSuccessResultList(list);
    }


}
